//
//  NSString+judge.m
//  ROL
//
//  Created by Mac on 2021/2/18.
//

#import "NSString+judge.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (judge)
/**
 *@pragma 判断是否手机号码
 */
- (BOOL)isMobile
{
    NSString *MOBILE = @"1[3456789]([0-9]){9}";
    NSPredicate *mobilePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [mobilePredicate evaluateWithObject:self];
}
/**
 *@pragma 判断是否纯数字
 */
- (BOOL)isNumber
{
    NSString *match = @"[0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}
- (BOOL)isBlank
{
    if (self == nil)
        return YES;
    else if ([self isKindOfClass:[NSNull class]])
        return YES;
    for(int i = 0; i < self.length; i++)
    {
        if([[self substringWithRange:NSMakeRange(i, 1)] isEqualToString:@" "]==NO)
            return NO;
    }
    return YES;
}
/**
 *@pragma 判断是否数字和字母混合
 */
- (BOOL)isNumberOrAlphabet
{
    NSString *match = @"^([a-zA-Z0-9]|[a-z]|[A-Z]|[0-9])+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}
- (BOOL)isNumOrChineseOrEnglish
{
    NSString * match = @"^[a-z0-9A-Z\u4e00-\u9fa5]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}
-(BOOL)isEmail

{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
- (CGSize)sizeWithFont:(UIFont *)font inWidth:(CGFloat )width
{
    CGSize size = CGSizeMake(width, MAXFLOAT);
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:font}
                                     context:nil];
    return CGSizeMake(ceil(rect.size.width), ceil(rect.size.height));
}
- (CGSize)sizeWithFont:(UIFont *)font
{
    CGSize size = [self sizeWithAttributes:@{NSFontAttributeName:((font)?font:[UIFont systemFontOfSize:[UIFont systemFontSize]])}];
    return CGSizeMake(ceil(size.width), ceil(size.height));
}
- (NSString *)transToTimeSp:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //设置本地时区
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:self];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];//时间戳
    return timeSp;
}
- (NSString *)transToTime:(NSString *)format
{
    NSTimeInterval time=[self doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    
    return currentDateStr;
}

- (NSString *)md5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr),digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    [result appendFormat:@"%02x", digest[i]];
    return result;
}
- (NSString *)chineseUrlString {
    return [self
     stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet
                                                         URLFragmentAllowedCharacterSet]];
}
@end
